package com.sunny.oj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.sunny.oj.common.ErrorCode;
import com.sunny.oj.exception.BusinessException;
import com.sunny.oj.exception.ThrowUtils;
import com.sunny.oj.mapper.PostCommentMapper;
import com.sunny.oj.model.entity.PostComment;
import com.sunny.oj.model.entity.PostFavour;
import com.sunny.oj.model.entity.PostThumb;
import com.sunny.oj.model.entity.User;
import com.sunny.oj.model.vo.PostCommentVO;
import com.sunny.oj.model.vo.PostVO;
import com.sunny.oj.model.vo.UserVO;
import com.sunny.oj.service.PostCommentService;
import com.sunny.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 帖子服务实现
 *
 * @author sunshuo
 */
@Service
@Slf4j
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment>
        implements PostCommentService {

    @Resource
    private UserService userService;

    @Resource
    private PostCommentMapper postCommentMapper;

    private final static Gson GSON = new Gson();

    @Override
    public void validPostComment(PostComment postComment, boolean add) {
        if (postComment == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String content = postComment.getContent();
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(content), ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isNotBlank(content) && content.length() > 150) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
        }
    }


    @Override
    public PostCommentVO getPostCommentVO(PostComment postComment, HttpServletRequest request) {
        PostCommentVO postCommentVO = PostCommentVO.objToVo(postComment);
        // 1. 关联查询用户信息
        Long userId = postComment.getUserId();
        User user = null;
        if (userId != null && userId > 0) {
            user = userService.getById(userId);
        }
        UserVO userVO = userService.getUserVO(user);
        postCommentVO.setUserVO(userVO);
        return postCommentVO;
    }

    @Override
    public List<PostComment> getList(Long postId) {
        return postCommentMapper.getList(postId);
    }

    @Override
    public List<PostCommentVO> getPostCommentVOList(List<PostComment> list, HttpServletRequest request) {
        List<PostCommentVO> commentVOList = new ArrayList<>();
        User user = null;
        for (PostComment postComment : list) {
            //PostVO postVO = PostVO.objToVo(post);
            PostCommentVO postCommentVO = PostCommentVO.objToVo(postComment);

            //long postId = post.getId();
            Long postCommentId = postComment.getId();

            // 1. 关联查询用户信息
            //Long userId = post.getUserId();
            Long userId = postComment.getUserId();

            if (userId != null && userId > 0) {
                user = userService.getById(userId);
            }
            UserVO userVO = userService.getUserVO(user);
            postCommentVO.setUserVO(userVO);

            commentVOList.add(postCommentVO);
        }

        return commentVOList;
    }

}




